package admin.code.generator.core.metadata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import admin.code.generator.common.ConstantTemplateKey;
import admin.code.generator.util.ClassNameMaker;

/**
 *
 *@author Jack
 *2016年3月3日下午11:55:25
 */
public class ViewMetaData {

	private String title;//页面名称

	private List<FieldProperty> properties;//页面属性,属性有重命名要求

	private String idName;
	private String modelId = "${model.id}";

	//private String message = "${message}";//view页面显示的新增|修改成功提示消息

	private String controllerMapping;//controller request mapping value

	//${model!=null && model.id!=null}? '修改游戏' : '新增游戏'"
	private String upsertText = "新增"; //form.html使用

	public ViewMetaData(TableMetaData tmd) {
		fromTableMataData(tmd);
	}

	public void fromTableMataData(TableMetaData tmd) {
		this.setIdName(tmd.getIdName());
		this.setModelId(tmd.getIdName());
		ClassNameMaker nameMaker = new ClassNameMaker(tmd.getTableName());
		this.setControllerMapping(nameMaker.controllerMappingVlaue());

		List<FieldProperty> pps = new ArrayList<FieldProperty>();
		for (ColumnMetaData cmd : tmd.getColumns()) {
			pps.add(new FieldProperty(cmd.getName(), cmd.getType(), cmd.getComment()));
		}
		this.setProperties(pps);
	}

	public Map fillTempateModelData() {
		Map root = new HashMap(8);
		root.put(ConstantTemplateKey.HTML_TITLE, this.getTitle());
		root.put(ConstantTemplateKey.HTML_PROPERTISE, this.getProperties());
		root.put(ConstantTemplateKey.ID_NAME, this.getIdName());
		root.put(ConstantTemplateKey.HTML_MODEL_ID, this.getModelId());
		//root.put(ConstantTemplateKey.HTML_MESSAGE, this.getMessage());
		root.put(ConstantTemplateKey.HTML_UPSERT_TEXT, this.getUpsertText());
		root.put(ConstantTemplateKey.REQUEST_MAPPING_VALUE, this.controllerMapping);
		return root;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setProperties(List<FieldProperty> properties) {
		this.properties = properties;
	}

	public List<FieldProperty> getProperties() {
		return properties;
	}

	public String getIdName() {
		return idName;
	}

	public void setIdName(String idName) {
		this.idName = idName;
	}

	public String getModelId() {
		return modelId;
	}

	public void setModelId(String modelId) {
		this.modelId = modelId;
	}

	/*public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}*/

	public String getControllerMapping() {
		return controllerMapping;
	}

	public void setControllerMapping(String controllerMapping) {
		this.controllerMapping = controllerMapping;
	}

	public void setUpsertText(String upsertText) {
		this.upsertText = upsertText;
	}

	public String getUpsertText() {
		return upsertText;
	}

}
